// pages/staff_info/staff_info.js
const App = getApp();
const Api = require('../../utils/api.js')
Page({

    /**
     * 页面的初始数据
     */
    data: {
      promise: [{
        value: '1',
        name: '您在办理安康码时，已承诺对填写信息的真实性负责且承担法律责任。'
      }, {
          value: '2',
          name: '如您不会申请安康码或未使用智能手机，可让亲友代申请。'
        },
            {
                value: '2',
                name: '欢迎您来太平猴魁唯一产地——黄山区务工，如遇到困难，请及时联系当地乡镇政府”。'
            },
        ],
        join: false,

        widfront: '', //身份证人像面
        needLog: false,
        invitor: null,
        showTxt:false,
        agree:false
    },
    agreeTxt(){
      this.setData({
        showTxt:false,
        agree:true,
      })
    },
    joinHandle() {
        let that = this
        that.setData({
            join: !that.data.join
        })
    },

    // 身份证人像面
    uploadImgFrontHandle() {
        let _this = this;
        // 记录formId
        // App.saveFormId(e.detail.formId);
        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success: function(res) {
                console.log(res)
                wx.uploadFile({
                    url: Api.API_URI + 'uploadImg',
                    filePath: res.tempFilePaths[0],
                    name: 'image',
                    success(res) {
                        console.log(res.data)
                        const data = JSON.parse(res.data)
                        _this.setData({
                            widfront: data.data.url
                        })
                    },
                    fail(e) {
                        console.log(e)
                    }
                })
            }
        });
    },

    // 提交表单
    formSubmit: function(e) {
        let _this = this;
        let info = e.detail.value;
        var reg = new RegExp(/^1(3|4|5|7|8)\d{9}$/);
        if(!_this.data.agree){
          _this.setData({
            showTxt:true
          })
          wx.showToast({
            title: '请同意用户协议',
            icon: 'none',
            duration: 2000
          })
          return;
        }
        if (!info.wname) {
            wx.showToast({
                title: '请输入姓名',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (!info.wphone) {
            wx.showToast({
                title: '请输入手机号码',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (!reg.exec(info.wphone)) {
            wx.showToast({
                title: "手机号格式错误",
                icon: 'none',
                duration: 1500,
            })
            return;
        }
        if (!info.wcardno) {
            wx.showToast({
                title: '请输入身份证号码',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (!info.waddress) {
            wx.showToast({
                title: '请输入身份证住址',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        if (!_this.data.widfront) {
            wx.showToast({
                title: '请上传安康码截图',
                icon: 'none',
                duration: 2000
            })
            return;
        }
        let data = {
            'wname': info.wname,
            'wphone': info.wphone,
            'wcardno': info.wcardno,
            'wregion': _this.data.formRegion,
            'waddress': info.waddress,
            'widfront': _this.data.widfront,
            'wdate':_this.data.date
        };
        data.eid = wx.getStorageSync("invitor")
        Api.fetch('signWorker', data)
            .then(res => {
                console.log(res.data);
                if (res.data.code == 1) {
                    wx.showToast({
                        title: res.data.msg,
                    })
                    App.globalData.userInfo = res.data.data.userInfo;
                    wx.setStorageSync("userInfo", res.data.data.userInfo);
                    wx.setStorageSync("usertype", res.data.data.userInfo.usertype);
                    setTimeout(() => {
                        wx.switchTab({
                            url: '/pages/staff/staff',
                        })
                    }, 1500)
                } else {
                    wx.showToast({
                        title: res.data.msg,
                        icon: "none"
                    })
                }
            })
            .catch(e => {
                console.log(e);
            })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        let invitor = options.scene;
        wx.setStorageSync("invitor", invitor) //存到缓存里

        //检查登陆状态
        let userInfo = wx.getStorageSync('userInfo')
        if (!userInfo) {
            wx.navigateTo({
                url: '/pages/login/login',
            })
        } else {
            //检查自己是否已经填写了信息
            this.checkNeed();
            this.getInvitor();

        }
    },
    checkNeed() {
        let that = this;
        Api.fetch('checkHas', {}).then(res => {
            that.setData({
                needLog: res.data.data.needLog
            })
        })
    },
    getInvitor() {
        let that = this;
        Api.fetch('getInvitor', {
            id: wx.getStorageSync("invitor")
        }).then(res => {
            that.setData({
                invitor: res.data.data.invitor
            })
        })
    },
    confirmLog() { //已经登记过信息的 直接确认就可以了;
        Api.fetch('confirmInvite', {
            id: wx.getStorageSync("invitor")
        }).then(res => {
            if (res.data.code == 0) {
                wx.showToast({
                    title: res.data.msg,
                    icon: 'none'
                })
            } else {
                wx.showToast({
                    title: res.data.msg
                })
                wx.reLaunch({
                    url: '/pages/main/main',
                })
            }

        })
    },
    bindRegionChange: function(e) {
        let region = e.detail.value.join("-")
        console.log(region)
        this.setData({
            region: e.detail.value,
            formRegion:region
        })
    },
    bindDateChange:function(e){
        this.setData({
            date: e.detail.value
        })
    },
    bindWayChange:function(e){
        let jtfs = this.data.jtfs;
        console.log(e.detail.value);
        this.setData({
            way: jtfs[e.detail.value]
        })
    },
    //复制皖事通下载链接
  copyurlwst() {
    wx.setClipboardData({
      data: "https://www.ahzwfw.gov.cn/",
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
  copyurlzfb() {
    wx.setClipboardData({
      data: "https://auth.alipay.com/login/index.htm",
      success: function (res) {
        wx.getClipboardData({
          success: function (res) {
            wx.showToast({
              title: '复制成功'
            })
          }
        })
      }
    })
  },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        this.checkNeed();
        this.getInvitor()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})